package com.wanmait.houlang.manageController;

import com.alibaba.fastjson.JSONObject;
import com.wanmait.houlang.pojo.DTO;
import com.wanmait.houlang.pojo.Manager;
import com.wanmait.houlang.service.ManagerService;
import com.wanmait.houlang.util.IgnoreToken;
import com.wanmait.houlang.util.JWTUtils;
import com.wanmait.houlang.util.Result;
import org.joda.time.DateTime;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Random;


@RestController
@CrossOrigin
public class CommonController {

    @Resource
    private ManagerService managerService;
    @Resource
    private RedisTemplate redisTemplate;

    //排除过滤器
    @IgnoreToken
    @PostMapping("/manage/login")
    public Result login(@RequestBody Manager manager,HttpServletRequest request){
        Manager loginManager = managerService.login(manager);
        if(loginManager==null){
            return Result.error("登录失败，用户名或者密码错误");
        }
        Random random = new Random();
        int randCode = random.nextInt(10000);
        //如果用户存在 生成token存在redis
        String telToken = JWTUtils.createTelToken(String.valueOf(randCode));
        redisTemplate.opsForValue().set(telToken,manager);
        //获取登录的ip
        String remoteAddr = request.getRemoteAddr();
        //设置登录的ip
        loginManager.setLastLoginIp(remoteAddr);
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        //设置登录的时间
        loginManager.setLastTime(timestamp);
        //更新管理员的登录信息
        managerService.update(loginManager);


        //将token返回到客户端创建token
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("token",telToken);
        jsonObject.put("manager",loginManager);
        return Result.success(jsonObject);
    }
    @IgnoreToken
    @PostMapping("/manage/telLogin")
    public Result login(@RequestBody DTO dto, HttpServletRequest request){
        //获取前端请求的token
        String token = request.getHeader("token");
        //根据请求携带的token去redis中查找对应的dto对象
        DTO loginInfo = (DTO)redisTemplate.opsForValue().get(token);
        //将redis中获取的手机号、验证码与前台请求中的进行对比
        if(loginInfo.getLoginCode().equals(dto.getLoginCode())&&loginInfo.getTelPhone().equals(dto.getTelPhone())){
            //根据手机号去数据库查找绑定的对象
            Manager manager = managerService.telLogin(dto);
            //如果查询的对象为空，说明该手机号未绑定账号
            if(manager==null){
                return Result.error("该手机号未绑定账号");
            }
            //获取登录的ip
            String remoteAddr = request.getRemoteAddr();
            //设置登录的ip
            manager.setLastLoginIp(remoteAddr);
            Date date = new Date();
            Timestamp timestamp = new Timestamp(date.getTime());
            //设置登录的时间
            manager.setLastTime(timestamp);
            //更新管理员的登录信息
            managerService.update(manager);

            //否则将管理员对象保存到redis中 返回成功代码
            redisTemplate.opsForValue().set(manager.getId(),manager);
            return Result.success(manager);

        }
        else{
            return Result.error("验证码输入有误");
        }
    }


}
